package com.example.controller;


import com.example.pojo.Dept;
import com.example.pojo.Result;
import com.example.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

/**
 * 部门管理Controller
 */
@Slf4j
@RestController
//@CrossOrigin(origins = "http://10.181.182.214:8082", allowCredentials = "true")
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptServicel;
    /*查询部门数据*/
    //private static Logger log= LoggerFactory.getLogger(DeptController.class);
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)//指定请求方式为get
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        //调用service 查询部门数据
        List<Dept> deptList=deptServicel.list();
        return Result.success(deptList);
    }

    /**
     * 删除部门
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门：{}",id);
        deptServicel.delete(id);
        return Result.success();
    }

    /**
     * 添加部门
     */
    @PostMapping
    public Result add(@RequestBody Dept dept)//@RequestBody json格式的封装
    {
        log.info("新增部门：{}",dept);
        //调用service新增部门
        deptServicel.add(dept);
        return Result.success();
    }

    /**
     * 更新部门
     * */
    @PutMapping
    public Result update(@RequestBody Dept dept)//@RequestBody json格式的封装
    {

        log.info("编辑部门：{}",dept);
        //调用service编辑部门
        deptServicel.update(dept);
        return Result.success();
    }
    /**
     * 通过id获取对象
     * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("获取部门id：{}",id);
        Dept dept = deptServicel.selectById(id);
        return Result.success(dept);
    }


}
